package com.compty.paint.web.front;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.compty.core.framework.util.StringUtil;
import com.compty.core.page.Page;
import com.compty.core.struts2.BaseAction;
import com.compty.paint.domain.Advertisement;
import com.compty.paint.domain.Bid;
import com.compty.paint.domain.CustomerInfo;
import com.compty.paint.domain.Goods;
import com.compty.paint.domain.Painting;
import com.compty.paint.manager.AdvertisementManager;
import com.compty.paint.manager.BidManager;
import com.compty.paint.manager.CustomerInfoManager;
import com.compty.paint.manager.GoodsManager;
import com.compty.paint.manager.PaintingManager;

/**
 * 瓷艺action
 * @author 朱庆辉
 */
public class CiYiAction  extends BaseAction<Painting> {

	private static final long serialVersionUID = 6119414991149215546L;
	
	private PaintingManager paintingManager;
	private GoodsManager goodsManager;
	private BidManager bidManager;
	private CustomerInfoManager customerInfoManager;
	private AdvertisementManager advertisementManager;
	
	private Painting  painting;
	private List<Advertisement> advertisementList; /*** 广告集合 ****/
	
	@SuppressWarnings("unchecked")
	public String index(){
		
    	Page page = paintingManager.findCiYiByPage(1,12,"1");//名师作品
    	List<Painting>  mszp = (List<Painting>)page.getResult(); 
    	request.setAttribute("mszp", mszp);
    	
    	page = paintingManager.findCiYiByPage(1,12,"2");//高仿精品
    	List<Painting>  gfjp = (List<Painting>)page.getResult(); 
    	request.setAttribute("gfjp", gfjp);
        
        page = advertisementManager.findByPage(curPage,3,"2");//瓷器频道广告
        this.advertisementList = (List<Advertisement>) page.getResult();
		
		return "index";
	}
	
	public String list(){
		
		String type = request.getParameter("type");
    	this.page = paintingManager.findCiYiByPage(curPage,24,type);
    	
		return "list";
	}
	
	/**
	 * 作品详情(以及商品详情、拍品详情)
	 */
	@SuppressWarnings("unchecked")
	public String detail() throws Exception {
		
			String idStr = request.getParameter("id");
			if( !StringUtil.isDigit(idStr) ){
				return ERROR;
			}
			Long id = Long.valueOf(idStr);
			this.painting = paintingManager.get(id);
			
			if( painting != null){
				
				Long[] idS = paintingManager.findPreAndNextId(id);
				int[] idArray = new int[2];
				for (int i = 0; i < idS.length; i++) {
					if(idS[i] != null){
						idArray[i] = idS[i].intValue();
					}else{
						idArray[i] = 0;
					}
				}
				request.setAttribute("paintingArray", "["+idArray[0]+","+idArray[1]+"]");
				
				/******************** 商品  begin  ***********************/
				Goods goods = goodsManager.findByPaintingId(id);
				if( goods != null ){
					request.setAttribute("goods", goods);
				}
				/******************** 商品  end  ***********************/
				
				/******************** 拍品  begin  ***********************/
				Bid bid = bidManager.findByPaintingId(id);
				if( bid != null ){
					bid = bidManager.findById(bid.getId());
					if(bid.getCurrentBidRecord() != null){
						if(bid.getCurrentBidRecord().getCustomerInfo() != null){
							Long c_id =  bid.getCurrentBidRecord().getCustomerInfo().getId();
							CustomerInfo customerInfo = customerInfoManager.get(c_id);
							bid.getCurrentBidRecord().setCustomerInfo(customerInfo);
						}
					}
					
					Date  endTime = bid.getEndTime();
					if(Calendar.getInstance().getTime().compareTo(endTime) < 0){
						bid.setNewFlag("1");
					}else{
						bid.setNewFlag("0");
					}
					request.setAttribute("bid", bid);
				}
				/******************** 拍品  end  ***********************/
				
				/******************** 是否显示预约信息 begin  ***********************/
				String onSaleFlag = painting.getOnSaleFlag();
				if("2".equals(onSaleFlag)){//表示此商品上架销售
					if( painting.getPainter() != null ){
						String remark = painting.getRemark();
						if("1".equals(remark)){
							Float price = painting.getPrice();
							if( price != null && price >= 15000){
								request.setAttribute("subscribeFlag", "1");
							}
						}
					}
				}
				/******************** 是否显示预约信息 end  *************************/
				
				getImagesArray(painting);
				
				Long  catalog_id = painting.getCatalogType().getId();
				List list = paintingManager.findRelativePaintingList(id,8,catalog_id);
				request.setAttribute("relativeList", list);
				
				if("1".equals(painting.getOnSaleFlag()) && bid != null){
					return "detail_bid";
				}else{
					return "detail";
				}
			}
		
		return null;
	}
	
	private void getImagesArray(Painting painting){
		StringBuffer sb = new StringBuffer("[");
		if(painting.getImageInfo() != null){
			sb.append("{originalImagePath:\""+painting.getOriginalImagePath()+"\",middleImageBpath:\""+painting.getMiddleImageBpath()+"\",smallImagePath:\""+painting.getSmallImagePath()+"\"}");
		}
		if(painting.getImageInfo2() != null){
			sb.append(",{originalImagePath:\""+painting.getOriginalImagePath2()+"\",middleImageBpath:\""+painting.getMiddleImageBpath2()+"\",smallImagePath:\""+painting.getSmallImagePath2()+"\"}");
		}
		if(painting.getImageInfo3() != null){
			sb.append(",{originalImagePath:\""+painting.getOriginalImagePath3()+"\",middleImageBpath:\""+painting.getMiddleImageBpath3()+"\",smallImagePath:\""+painting.getSmallImagePath3()+"\"}");
		}
		if(painting.getImageInfo4() != null){
			sb.append(",{originalImagePath:\""+painting.getOriginalImagePath4()+"\",middleImageBpath:\""+painting.getMiddleImageBpath4()+"\",smallImagePath:\""+painting.getSmallImagePath4()+"\"}");
		}
		sb.append("]");
		request.setAttribute("imagesArray", sb.toString());
	}

	public void setPaintingManager(PaintingManager paintingManager) {
		this.paintingManager = paintingManager;
	}

	public Painting getPainting() {
		return painting;
	}

	public void setPainting(Painting painting) {
		this.painting = painting;
	}

	public void setGoodsManager(GoodsManager goodsManager) {
		this.goodsManager = goodsManager;
	}

	public void setBidManager(BidManager bidManager) {
		this.bidManager = bidManager;
	}

	public void setCustomerInfoManager(CustomerInfoManager customerInfoManager) {
		this.customerInfoManager = customerInfoManager;
	}

	public List<Advertisement> getAdvertisementList() {
		return advertisementList;
	}

	public void setAdvertisementList(List<Advertisement> advertisementList) {
		this.advertisementList = advertisementList;
	}

	public void setAdvertisementManager(AdvertisementManager advertisementManager) {
		this.advertisementManager = advertisementManager;
	}
}
